//验证token中间件
const {jwtSecret}=require('../config/config.default')
const { verify } = require('../util/jwt')
const{User}=require('../model')
module.exports= async (req,res,next)=>{
  let token=req.headers.authorization
  token=token ? token.split('Bearer ')[1]:null
 if(!token){
      res.status(401).end()
 }
  try {
    const decodetoken= await verify(token,jwtSecret)
    console.log(decodetoken)
   req.user= await User.findById(decodetoken.userID)
    next()
  } catch (error) {
      return res.status(401).end()
  }
}